package logic

import (
	"2210a/commod/commodrpc/commodrpc"
	"2210a/commod/commodrpc/internal/svc"
	"2210a/commod/models/mysql"
	"2210a/commod/models/redisinit"
	"context"
	"encoding/json"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type ShopLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopLikeLogic {
	return &ShopLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopLikeLogic) ShopLike(in *commodrpc.ShopLikeRequest) (*commodrpc.ShopLikeResponse, error) {
	// todo: add your logic here and delete this line

	var FindProduct mysql.Products
	mysql.Db.Debug().Where("id = ?", in.Id).Find(&FindProduct)
	if FindProduct.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}
	if FindProduct.UserId != in.UserId {
		return nil, fmt.Errorf("用户不存在，点赞失败")
	}
	marshal, _ := json.Marshal(&FindProduct)
	//FindProduct.Likes = FindProduct.Likes + 1
	//mysql.Db.Debug().Model(&FindProduct).Updates(&FindProduct)

	redisinit.ClientRedis.ZIncrBy("ShopLike", 1, string(marshal))

	return &commodrpc.ShopLikeResponse{Id: int64(FindProduct.ID)}, nil
}

//{
//	"code": 200,
//	"message": "商品点赞成功",
//	"data": 5
//}
